package basiclocaloperator.parsexml;

import org.junit.Test;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class ParseXml {
    public static void main(String[] args) throws Exception {

    }

    @Test
    public void read() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("./web/xmldtd/elementeg/book.xml");
        NodeList books = document.getElementsByTagName("书名");
        for (int i = 0; i < books.getLength(); i++) {
            Node node = books.item(i);
            System.out.println(node.getTextContent());
        }
        Node root = document.getElementsByTagName("书架").item(0);
        list(root);
    }

    private void list(Node node) {
        if (node instanceof Element)
            System.out.println(node.getNodeName());

        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            list(child);
        }
    }

    @Test
    public void add() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("./web/xmldtd/elementeg/book.xml");

        Element price = document.createElement("售价");
        price.setTextContent("59");

        document.getElementsByTagName("书").item(0).appendChild(price);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("./web/xmldtd/elementeg/book.xml"));
    }

    @Test
    public void insert() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("./web/xmldtd/elementeg/book.xml");

        Element price = document.createElement("售价");
        price.setTextContent("59");

        Element book = (Element) document.getElementsByTagName("书").item(1);
        Node ref = book.getElementsByTagName("售价").item(0);

        book.insertBefore(price, ref);


        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("./web/xmldtd/elementeg/book.xml"));

    }

    @Test
    public void update() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("./web/xmldtd/elementeg/book.xml");

        Element node = (Element) document.getElementsByTagName("售价").item(0);
        node.setTextContent("110");

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("./web/xmldtd/elementeg/book.xml"));
    }

    @Test
    public void delete() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("./web/xmldtd/elementeg/book.xml");

        document.getElementsByTagName("书").item(0).removeChild(document.getElementsByTagName("售价").item(0));

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("./web/xmldtd/elementeg/book.xml"));
    }
}
